package com.musiklegal;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import android.util.Log;



public class InboxPar extends DefaultHandler 
{
	
	boolean date=false,name=false,mess=false,entry =false;
	StringBuffer buffer;
	InboxData data;
	private boolean track;
	private boolean idring;
	private boolean artist;
	private boolean artist_id;
	private boolean al_art;
	private boolean al_name;
	private boolean year;
	private boolean publisher;
	private boolean genre;
	private boolean composer;
	private boolean song;
	private boolean label;
	private boolean preview;
	private boolean api;
	private boolean rating;
	
	
	InboxPar()
	{
		buffer= new StringBuffer();
		
	}
	
	
	public void startDocument() throws SAXException
	{
	}
	
	public void endDocument() throws SAXException
	{
		System.gc();	
	}

	public void startElement(String namespaceURI, String localName, String qName, Attributes atts) throws SAXException
	{
		if(localName.equals("messages"))
		{
			entry=true;
			data = new InboxData();
		}
		else if(localName.equals("date"))
			date=true;
		else if(localName.equals("from"))
			{
				data.username = atts.getValue("username");
				name=true;
			}
		else if(localName.equals("message"))
			mess=true;
		
		else if(localName.equals("track"))
		{
			data.attachment = new DataSong();
			track = true;
		}
		else if(localName.equals("id_ring")&&track)
			idring=true;
		else if(localName.equals("artist")&&track)
			artist = true;
		else if(localName.equals("artis_id")&&track)
			artist_id=true;
		else if(localName.equals("song")&&track)
			song=true;
		else if(localName.equals("composer")&&track)
			composer=true;
		else if(localName.equals("genre")&&track)
			genre=true;
		else if(localName.equals("publisher")&&track)
			publisher=true;
		else if(localName.equals("year")&&track)
			year=true;
		else if(localName.equals("album_name")&&track)
			al_name=true;
		else if(localName.equals("album_art")&&track)
			al_art = true;
		else if(localName.equals("label")&&track)
			label=true;
		else if(localName.equals("preview")&&track)
			preview = true;
		else if(localName.equals("merchant_api")&&track)
			api=true;
		else if(localName.equals("rating")&&track)
			rating=true;
		
		
	}
	
	
	public void endElement(String namespaceURI, String localName, String qName)
	throws SAXException 
	{
		
		if(localName.compareToIgnoreCase("messages")==0&&entry)
		{
			Inbox.ibd.add(0,data);
			data=null;
			entry = false;
		}
		else if(localName.equals("date")&&entry)
			{
			data.date = buffer.toString();
			buffer = new StringBuffer();
				date=false;
			}
		else if(localName.equals("from")&&entry)
			{
			data.name = buffer.toString();
			buffer = new StringBuffer();
				name=false;
			}
		else if(localName.equals("message")&&entry)
			{
			data.mess = buffer.toString();
			buffer = new StringBuffer();
				mess=false;
			}
		
		else if(localName.equals("track"))
			{
			//Log.d("tes", data.attachment.song+" "+data.attachment.artist_id);
				if(data.attachment.albid==null)
				{
					String[] t = data.attachment.album.split("/");
					data.attachment.albid = t[t.length-1];
				}
				track=false;
			
			}
		else if(localName.equals("id_ring")&&track)
			{
				data.attachment.idring = buffer.toString();
				buffer = new StringBuffer();
				idring = false;
			}
		else if(localName.equals("artist")&&track)
			{
				data.attachment.artist = buffer.toString();
				buffer = new StringBuffer();
				artist = false;
			}
		else if(localName.equals("artis_id")&&track)
		{
			data.attachment.artist_id = buffer.toString();
			buffer = new StringBuffer();
			artist_id=false;
		}
		else if(localName.equals("song")&&track)
			{
				data.attachment.song = buffer.toString();
				buffer = new StringBuffer();
				song=false;
			}
		else if(localName.equals("composer")&&track)
			{
				data.attachment.composer = buffer.toString();
				buffer = new StringBuffer();
				composer=false;
			}
		else if(localName.equals("genre")&&track)
			{
				data.attachment.genre = buffer.toString();
				buffer = new StringBuffer();
				genre=false;
			}
		else if(localName.equals("publisher")&&track)
			{
				data.attachment.pub = buffer.toString();
				buffer = new StringBuffer();
				publisher=false;
			}
		else if(localName.equals("year")&&track)
			{
				data.attachment.year = buffer.toString();
				buffer = new StringBuffer();
				year=false;
			}
		else if(localName.equals("album_name")&&track)
			{
				data.attachment.alname = buffer.toString();
				buffer = new StringBuffer();
				al_name=false;
			}
		else if(localName.equals("album_art")&&track)
			{
				data.attachment.album = buffer.toString();
				buffer = new StringBuffer();
				al_art = false;
			}
		else if(localName.equals("label")&&track)
			{
				data.attachment.label = buffer.toString();
				buffer = new StringBuffer();
				label=false;
			}
		else if(localName.equals("preview")&&track)
			{
			data.attachment.preview = buffer.toString();
			buffer = new StringBuffer();
				preview = false;
			}
		else if(localName.equals("merchant_api")&&track)
			{
				data.attachment.api = buffer.toString();
				buffer = new StringBuffer();
				api=false;
			}
		else if(localName.equals("rating")&&track)
			{
				data.attachment.rating = buffer.toString();
				buffer = new StringBuffer();
				rating=false;
			}
		
		
		

	}
	
	 public void characters(char ch[], int start, int length) 
	 {
		 
		 if(date)
		 {
			 //Log.d("ZZZZZZZZ",new String(ch,start,length));
			 buffer.append(new String(ch,start,length));
		 }
		 else if(name)
		 {
			 //Log.d("ZZZZZZZZ",new String(ch,start,length));
			 buffer.append(new String(ch,start,length));
		 
		 }
		 else if(mess)
		 {
			 //Log.d("ZZZZZZZZ",new String(ch,start,length));
			 buffer.append(new String(ch,start,length));
		 }
		 else if(idring)
		 {
			 //Log.d("ZZZZZZZZ",new String(ch,start,length));
			 buffer.append(new String(ch,start,length));
		 }
		 else if(song)
		 {
			 //Log.d("ZZZZZZZZ",new String(ch,start,length));
			 buffer.append(new String(ch,start,length));
		 }
		 else if(artist)
		 {
			 //Log.d("ZZZZZZZZ",new String(ch,start,length));
			 buffer.append(new String(ch,start,length));
		 }
		 else if(artist_id)
		 {
			 //Log.d("ZZZZZZZZ",new String(ch,start,length));
			 buffer.append(new String(ch,start,length));
		 }
		 else if(al_name)
		 {
			 //Log.d("ZZZZZZZZ",new String(ch,start,length));
			 buffer.append(new String(ch,start,length));
		 }
		 else if(al_art)
		 {
			 //Log.d("ZZZZZZZZ",new String(ch,start,length));
			 buffer.append(new String(ch,start,length));
		 }
		 else if(preview)
		 {
			 //Log.d("ZZZZZZZZ",new String(ch,start,length));
			 buffer.append(new String(ch,start,length));
		 }
		 else if(year)
		 {
			 //Log.d("ZZZZZZZZ",new String(ch,start,length));
			 buffer.append(new String(ch,start,length));
		 }
		 else if(api)
		 {
			 //Log.d("ZZZZZZZZ",new String(ch,start,length));
			 buffer.append(new String(ch,start,length));
		 }
		 else if(label)
		 {
			 //Log.d("ZZZZZZZZ",new String(ch,start,length));
			 buffer.append(new String(ch,start,length));
		 }
		 else if(publisher)
		 {
			 //Log.d("ZZZZZZZZ",new String(ch,start,length));
			 buffer.append(new String(ch,start,length));
		 }
		 else if(composer)
		 {
			 //Log.d("ZZZZZZZZ",new String(ch,start,length));
			 buffer.append(new String(ch,start,length));
		 }
		 else if(genre)
		 {
			 //Log.d("ZZZZZZZZ",new String(ch,start,length));
			 buffer.append(new String(ch,start,length));
		 }
		 else if(rating)
		 {
			 //Log.d("ZZZZZZZZ",new String(ch,start,length));
			 buffer.append(new String(ch,start,length));
		 }
	 }
}